import React from 'react';
import PropTypes from 'prop-types';
// eslint-disable-next-line import/no-named-default
import { default as CG } from '@/components/SlickUpload';

// TODO: 上传和删除附件的异步url 可配置

function SlickUpload({ onChange, value, accept, size, addUrl, delUrl, ...rest }) {
  const extra = { ...rest };

  if (accept.length > 0) {
    extra.accept = accept.join(',');
  }

  if (Array.isArray(value)) {
    extra.fileList = value.map(item => {
      return {
        uid: item.attachId,
        name: item.fileName,
        url: item.url,
        status: 'done',
        ...item,
      };
    });
  }

  // M转化成byte
  if (size) {
    extra.size = size * 1024 * 1024;
  }

  return (
    <>
      <CG action={addUrl} deleteUrl={delUrl} {...extra} />
    </>
  );
}

SlickUpload.defaultProps = {
  addUrl: '',
  delUrl: '',
  length: 9,
  size: 10,
  listType: 'text',
  method: 'post',
  accept: [],
  onRemove: () => {},
  multiple: false,
};

SlickUpload.propTypes = {
  addUrl: PropTypes.string,
  delUrl: PropTypes.string,
  length: PropTypes.number,
  size: PropTypes.number,
  listType: PropTypes.oneOf(['text', 'picture', 'picture-card']),
  method: PropTypes.oneOf(['get', 'post']),
  accept: PropTypes.array,
  onRemove: PropTypes.func,
  multiple: PropTypes.bool,
};

export default SlickUpload;
